package top.zsyle.basic.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import top.zsyle.org.dto.ShopAuditDto;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Slf4j
public class EmailUtil {

    private static JavaMailSender javaMailSender = SpringUtil.getBean(JavaMailSender.class);

    public static void sendShopEmail(ShopAuditDto dto,String text,String path){
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1351896179@qq.com");
            helper.setSubject("宠物乐园审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>"+text+"</h1>"
                    +path+dto.getShopId(),true);
            // 5.设置收件人
            helper.setTo("1351896179@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送邮件失败:{}",e);
//            e.printStackTrace();
        }
    }

    public static void sendEmailCode(String emailCode){
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1351896179@qq.com");
            helper.setSubject("宠物乐园邮箱");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>验证码："+emailCode+"</h1>",true);
            // 5.设置收件人
            helper.setTo("1351896179@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送邮件失败:{}",e);
//            e.printStackTrace();
        }
    }

    public static void sendPasswordEmail(){
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1351896179@qq.com");
            helper.setSubject("宠物乐园邮箱");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>默认密码：123456</h1>",true);
            // 5.设置收件人
            helper.setTo("1351896179@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送邮件失败:{}",e);
//            e.printStackTrace();
        }
    }
}
